package tatoeba

import inseniarlingua.trans.en_ia.Translator;
import groovy.transform.CompileStatic;
import groovy.transform.ToString;

@CompileStatic
class CsvReader extends BufferedReader {

	public CsvReader(File file) {
		super(new FileReader(file))
	}
	
	String[] readLineParts() {
		final line = readLine();
		if (line == null) return null
		return line.split(/\t/)
	}

	SentenceEntry readSentenceEntry() {
		final parts = readLineParts()
		if (parts == null) return null

		if (parts.length != 3) {
//			println parts
			throw new IllegalFormatException()
		}
		//		println parts
		return new SentenceEntry(parts[0].toInteger(), parts[1], parts[2])
	}
	
	TransEntry readTransEntry() {
		final parts = readLineParts()
		if (parts == null) return null

		if (parts.length != 2) {
			println parts
			throw new IllegalFormatException()
		}
		//		println parts
		return new TransEntry(parts[0].toInteger(), parts[1].toInteger())
	}

	static void main(String[] args) {
		final file = new File("c:\\Users\\janni\\Documents\\sentences.csv")
		def reader = new CsvReader(file)
		int i = 0

		// Fill inaMap
		SentenceEntry e;
		final inaMap = [:]
		while (e=reader.readSentenceEntry()) {
			if (e.langCode != 'ina') continue
//				println e
			//			if (i++ > 10) break
			inaMap[e.id] = e.sentence;
		}
		println "inaMap.size() = ${inaMap.size()}"
		reader.close()
		
		// Read transFile
		File transFile = new File("c:\\Users\\janni\\Documents\\links.csv")
		def transReader = new CsvReader(transFile)
		final Map<Integer,Integer> eng2InaIdMap = [:]
		TransEntry te
		while (te=transReader.readTransEntry()) {
			if (inaMap.containsKey(te.translationId)) { // eng --> ina
				eng2InaIdMap[te.sentenceId] = te.translationId
			}
//			if (inaMap.containsKey(te.sentenceId)) { // ina --> eng
//				eng2InaIdMap[te.translationId] = te.translationId
//			}
		}
		println "eng2InaIdMap.size() = ${eng2InaIdMap.size()}"
		transReader.close()
		
		// Make eng2InaMap
		reader = new CsvReader(file)
		final eng2InaMap = [:]
		while (e=reader.readSentenceEntry()) {
			if (e.langCode != 'eng') continue
			if (eng2InaIdMap.containsKey(e.id)) {
				final ina = inaMap[eng2InaIdMap[e.id]]
				eng2InaMap[e.sentence] = ina
			}
		}
		println "eng2InaMap.size() = ${eng2InaMap.size()}"
		Translator translator = new Translator()
		eng2InaMap.each { String eng, String ina ->
			if (! eng.contains('breeze')) return
			println "$eng --> $ina"
			final transToIna = translator.translate(eng.toLowerCase())
			println transToIna;
			println ""
		}
		

		reader.close()
	}
}

@ToString
class SentenceEntry {
	int id
	String langCode
	String sentence
	public SentenceEntry(int id, String langCode, String sentence) {
		super();
		this.id = id;
		this.langCode = langCode;
		this.sentence = sentence;
	}
}

@ToString
class TransEntry {
	final int sentenceId
	final int translationId

	public TransEntry(int sentenceId, int translationId) {
		super();
		this.sentenceId = sentenceId;
		this.translationId = translationId;
	}
}
